home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / heapst10.zip / HEAPSTR.PAS < prev    next >
Pascal/Delphi Source File  |  1993-01-04  |  2KB  |  99 lines

  1. UNIT heapstr;
  2.  
  3.  
  4. {Unit for dynamic strings (stored on the heap), version 1.0.
  5. Written for Turbo Pascal 5.5.
  6.  
  7. Donated to the public domain by Wayne E. Conrad, January, 1989.
  8. If you have any problems or suggestions, please contact me at my BBS:
  9.  
  10.     Pascalaholics Anonymous
  11.     (602) 484-9356
  12.     2400 bps
  13.     The home of WBBS
  14.     Lots of source code
  15. }
  16.  
  17.  
  18.  
  19. INTERFACE
  20.  
  21.  
  22. {Here's the object for dynamic strings.  It's bad form to access the
  23. "strptr" variable directly.  Instead, use the function "value" to get
  24. the current value.}
  25.  
  26. TYPE
  27.   heap_string =
  28.     OBJECT
  29.     strptr: ^String;
  30.     CONSTRUCTOR init;
  31.     FUNCTION store (st: String): Boolean;
  32.     FUNCTION value: String;
  33.     DESTRUCTOR dispose;
  34.     END;
  35.  
  36.  
  37. IMPLEMENTATION
  38.  
  39.  
  40. {This method initializes the heap string, clearing it.  This method must
  41. be called before any of the other methods are used, and it should not be
  42. called after that.}
  43.  
  44. CONSTRUCTOR heap_string.init;
  45. BEGIN
  46.   strptr := NIL
  47. END;
  48.  
  49.  
  50.  
  51. {Set the value of the string on the heap.  If not enough memory is
  52. available, returns False.}
  53.  
  54. FUNCTION heap_string.store (st: String): Boolean;
  55. VAR
  56.   nbytes: Word;
  57. BEGIN
  58.   dispose;
  59.   nbytes := Succ (Length (st) );
  60.   IF MaxAvail < nbytes THEN
  61.     store := False
  62.   ELSE
  63.     BEGIN
  64.     GetMem (strptr, nbytes);
  65.     Move (st, strptr^, nbytes);
  66.     store := True
  67.     END;
  68. END;
  69.  
  70.  
  71. {Get the value of the string.  If the string doesn't exist, returns an
  72. empty string.}
  73.  
  74. FUNCTION heap_string.value: String;
  75. BEGIN
  76.   IF strptr = NIL THEN
  77.     value := ''
  78.   ELSE
  79.     value := strptr^
  80. END;
  81.  
  82.  
  83. {Dispose of the string, if it exists.}
  84.  
  85. DESTRUCTOR heap_string.dispose;
  86. VAR
  87.   nbytes: Word;
  88. BEGIN
  89.   IF strptr <> NIL THEN
  90.     BEGIN
  91.     nbytes := Succ (Length (strptr^) );
  92.     FreeMem (strptr, nbytes);
  93.     strptr := NIL
  94.     END
  95. END;
  96.  
  97.  
  98. END.
  99.